define(function (require, exports, module) {
    'use strict';

    function ctrl($scope,$state,$stateParams,$filter,$ionicHistory,CommonService,InfoService,HistoryNavChartService,
                  $ionicScrollDelegate,$ionicLoading) {

        var par = $scope.param = new Array();
        par.chartConfig = HistoryNavChartService.historyYieldChart();

        $scope.$on('$ionicView.beforeEnter', function() {
            par.duration_detail = false; //是否显示期限详情
            par.show_chart = false; //是否展示走势图
            par.prod_code = $stateParams['prod_code'];

            init();
        });

        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        $scope.onBackKeyDown = function(){
            if($scope.param.duration_detail) {
                $scope.param.duration_detail = false;
                $ionicScrollDelegate.scrollTo(0,par.scrollTop,false);
            } else {
                if($ionicHistory.backView() == null) {
                    $state.go("tab.index");
                } else if($ionicHistory.backView().stateName == "tab.search"){
                    $ionicHistory.goBack(-2);
                }else if($ionicHistory.backView().stateName == "tab.product-item-QDII"){
                    $state.go("tab.index");
                }  else {
                    $ionicHistory.goBack();
                }
            }

        };

        function init(){
            if(!$scope.fund){
                $ionicLoading.show();
            }
            return InfoService.getFundGeneral(par.prod_code).then(function(data){
                if(data){
                    $scope.fund = data;
                    par.title = $scope.fund.prod_name;
                    var prod_profit_mode= $scope.fund.prod_profit_mode;
                    if("0"==prod_profit_mode){
                        //绘制七日年化走势图
                        par.show_chart = true;
                        drawChart($scope.fund.prod_id);
                    }else if("1"==prod_profit_mode){
                        $state.go("tab.product-item-QDII", {prod_code : $scope.fund.prod_code});
                    }else if('2,3'.indexOf(prod_profit_mode) < 0){
                        CommonService.showConfig({message:"产品收益模式未配置:"+ $scope.fund.prod_code});
                    }
                    $scope.fund.prodMinSubscribe_show = $filter('prodMinSubscribe')(CommonService.getMinAmount($scope.fund));
                }
                $ionicLoading.hide();
            });
        }

        $scope.showProdDurationDetail = function(){
            if(!CommonService.isStrEmpty($scope.fund.prod_duration_desc)){
                if($scope.fund.prod_status == 4){
                    par.title = "认购期说明";
                }else if($scope.fund.prod_status == 5){
                    par.title = "封闭期说明";
                }else{
                    par.title = "期限说明";
                }
                par.duration_detail = true;
                par.scrollTop=$ionicScrollDelegate.getScrollPosition().top;
                $ionicScrollDelegate.scrollTop(false);
            }
        };

        function drawChart(code){
            InfoService.getHistoryYield(code,20).then(function(data){
                if(data && data.length > 0){
                    //绘制七日年化走势图
                    par.chartConfig = HistoryNavChartService.historyYieldChart(data);
                    par.lastTime = par.chartConfig.series[0].data.pop()[0];
                } else {
                    par.show_chart = false;
                }
            });
        };
    }

    ctrl.$inject = ['$scope','$state','$stateParams','$filter','$ionicHistory','CommonService','InfoService','HistoryNavChartService',
        '$ionicScrollDelegate','$ionicLoading'];
    return ctrl;
});